﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WHControlLib.HardWare.PosPrinter
{
    /// <summary>
    /// POS打印机ESC指令封装基类
    /// </summary>
   public class ESCPrinterOrder
    { const Char ESC = (char)27;

        /// <summary>
        /// 设置初始化打印机
        /// </summary>
        /// <returns></returns>
        public string Set_PrinterIni()
        {
            char[] Ini= new char[] { (char)27,(char)64 };
            string INI = new string(Ini);
            return INI;

        }

      /// <summary>
      /// 返回打印缓冲区能的内容并进纸N行
      /// </summary>
      /// <param name="LineCount">进空白纸的行数</param>
      /// <returns></returns>
        public string Get_PrintAndGoBlankLine(int LineCount)
        {
              StringBuilder sb = new StringBuilder();
                sb.Append(ESC);
                sb.Append((char)100);
              
            if (LineCount>=0&&LineCount<=255)
                     sb.Append(LineCount.ToString());
            else
        
                sb.Append('0');

         return sb.ToString();

        }

        /// <summary>
        /// 返回将打印缓冲区中的数据打印出来，并且按照当前行间距，把打印纸向前推进一行
        /// </summary>
        /// <returns>控制字符串</returns>
        public string Get_PrintAndNewLine()
        {
            char[] LF = new char[] { (char)10};
            string lf = new string(LF);
            return lf;
        }
        /// <summary>
        /// 返回打印输出打印缓冲区中的数据并进纸 [n X 0.125 毫米]。
        /// </summary>
        /// <param name="n">0到255之间</param>
        /// <returns></returns>
        public string Get_PrintAndGoNmm(int n)

        {
            StringBuilder sb = new StringBuilder();
            sb.Append(ESC);
            sb.Append((char)74);

            if (n >= 0 && n <= 255)
                sb.Append(n.ToString());
            else

                sb.Append('0');

            return sb.ToString();

        }
        /// <summary>
        /// 返回打印机开钱箱指令
        /// </summary>
        /// <param name="DrawCashOrder">如为NULL返回通用开钱箱指令，自定义开钱箱指令</param>
        /// <returns></returns>
        public string Get_PrinterDrawCash(string DrawCashOrder)
        {
            if (DrawCashOrder==null)
            {
             char[] drawcash = new char[] { (char)27,(char)112,(char)0,(char)100,(char)100 };
               
            string Drawcash = new string(drawcash);
            return Drawcash;
            }
            else
            {
              string[] drawcash = DrawCashOrder.Split(',');
                   StringBuilder sb = new StringBuilder(); 
                for (int i = 0; i < drawcash.Length; i++)
                {
               
                    sb.Append(drawcash[i]);
                }
                return sb.ToString();
            }
      

        }
    
    public string Get_ToChineseMode()
        {
            char[] order = new char[] { (char)28, (char)38 };
            string OrderStr = new string(order);
            return OrderStr;
        }
        public string Get_ToEnglishMode()
        {
            char[] order = new char[] { (char)28, (char)46 };
            string OrderStr = new string(order);
            return OrderStr;
        }

      /// <summary>
      /// 获得发送串口可接受的中文编码字节数组
      /// </summary>
      /// <param name="text">发送的文本</param>
      /// <param name="bytesLength">输出 文本字节数组的长度</param>
      /// <param name="GBcode">要求的编码名称 ，如果为null，默认GB2312</param>
      /// <returns>要发送中文编码字节数组</returns>
        public byte[] Get_COMPortSendText(string text,out int bytesLength,string GBcode)
        {
            string codename = "GB2312";
            if (GBcode!=null)
            {
                codename=GBcode;
            }
            byte[] data = new byte[text.Length];
       
            Encoding GB = Encoding.GetEncoding("GB2312");
            byte[] bytes = GB.GetBytes(text);
            bytesLength= bytes.Length;
            return bytes;


        }
        /////////////////////////  

    }
}
